package mn.slide.dailynews.helper;

import java.sql.SQLException;

import mn.slide.dailynews.model.Tv;
import mn.slide.dailynews.model.TvDay;
import mn.slide.dailynews.model.TvDayItem;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * Copyright [2012] [Энхтөр]
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
public class DatabaseHelper extends OrmLiteSqliteOpenHelper
{
	private static final String		DATABASE_NAME		= "show.sqlite";
	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int		DATABASE_VERSION	= 1;

	// the DAO object we use to access the SimpleData table
	private Dao<Tv, Integer>		tvDao				= null;
	private Dao<TvDayItem, Integer>	tvDayItemDao		= null;
	private Dao<TvDay, Integer>		tvDayDao			= null;

	public DatabaseHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * This is called when the database is first created. Usually you should
	 * call createTable statements here to create the tables that will store
	 * your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource)
	{
		try
		{
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, Tv.class);
			TableUtils.createTable(connectionSource, TvDayItem.class);
			TableUtils.createTable(connectionSource, TvDay.class);
		}
		catch (SQLException e)
		{
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher
	 * version number. This allows you to adjust the various data to match the
	 * new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion)
	{
		try
		{
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.createTable(connectionSource, Tv.class);
			TableUtils.createTable(connectionSource, TvDayItem.class);
			TableUtils.createTable(connectionSource, TvDay.class);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		}
		catch (SQLException e)
		{
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It
	 * will create it or just give the cached value.
	 */
	public Dao<Tv, Integer> getTvDao() throws SQLException
	{
		if ( tvDao == null )
			tvDao = getDao(Tv.class);
		return tvDao;
	}

	public Dao<TvDay, Integer> getTvDayDao() throws SQLException
	{
		if ( tvDayDao == null )
			tvDayDao = getDao(TvDay.class);
		return tvDayDao;
	}

	public Dao<TvDayItem, Integer> getTvDayItemDao() throws SQLException
	{
		if ( tvDayItemDao == null )
			tvDayItemDao = getDao(TvDayItem.class);
		return tvDayItemDao;
	}

	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close()
	{
		super.close();
		tvDao = null;
		tvDayDao = null;
		tvDayItemDao = null;
	}
}
